package com.cucn.eduservice.controller;


import com.cucn.commonutils.Result;
import com.cucn.eduservice.entity.EduChapter;
import com.cucn.eduservice.entity.chapter.ChapterVO;
import com.cucn.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author KouChaoJie
 * @since 2021-09-29
 */
//@CrossOrigin
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    /**
     * 课程大纲列表
     * 根据课程id进行查询
     *
     * @param courseId 课程id
     */
    @GetMapping("/getChapterVideo/{courseId}")
    public Result getChapterVideo(@PathVariable("courseId") String courseId) {
        List<ChapterVO> list = chapterService.getChapterVideoByCourseId(courseId);

        return Result.ok().data("allChapterVideo", list);
    }

    /**
     * 添加章节
     *
     * @param eduChapter 添加的章节对象
     */
    @PostMapping("/addChapter")
    public Result addChapter(@RequestBody EduChapter eduChapter) {
        chapterService.save(eduChapter);

        return Result.ok();
    }

    /**
     * 根据章节id查询
     *
     * @param chapterId 章节id
     */
    @GetMapping("/getChapterInfo/{chapterId}")
    public Result getChapterInfo(@PathVariable String chapterId) {
        EduChapter eduChapter = chapterService.getById(chapterId);

        return Result.ok().data("chapter", eduChapter);
    }

    /**
     * 修改章节
     *
     * @param eduChapter 修改的章节信息
     */
    @PostMapping("/updateChapter")
    public Result updateChapter(@RequestBody EduChapter eduChapter) {
        chapterService.updateById(eduChapter);

        return Result.ok();
    }

    /**
     * 删除章节
     * @param chapterId 删除章节的id
     */
    @DeleteMapping("{chapterId}")
    public Result deleteChapter(@PathVariable String chapterId){
        boolean flag=chapterService.deleteChapter(chapterId);
        if (flag){
            return Result.ok();
        }else {
            return Result.error();
        }
    }
}

